﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace StudentDB
{
    public partial class StudentDBContext : DbContext
    {
        public StudentDBContext()
        {
        }

        public StudentDBContext(DbContextOptions<StudentDBContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Course> Courses { get; set; }
        public virtual DbSet<Sc> Scs { get; set; }
        public virtual DbSet<Student> Students { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("server=localhost;uid=sa;pwd=201219;database=StudentDB;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<Course>(entity =>
            {
                entity.HasKey(e => e.Cno);

                entity.ToTable("Course");

                entity.Property(e => e.Cno)
                    .HasMaxLength(2)
                    .IsFixedLength(true);

                entity.Property(e => e.Cname).HasMaxLength(20);
            });

            modelBuilder.Entity<Sc>(entity =>
            {
                entity.ToTable("SC");

                entity.HasIndex(e => new { e.Sno, e.Cno }, "IX_SC")
                    .IsUnique();

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Cno)
                    .HasMaxLength(2)
                    .IsFixedLength(true);

                entity.Property(e => e.Grade).HasColumnName("grade");

                entity.Property(e => e.Sno)
                    .HasMaxLength(9)
                    .IsFixedLength(true);

                entity.HasOne(d => d.CnoNavigation)
                    .WithMany(p => p.Scs)
                    .HasForeignKey(d => d.Cno)
                    .HasConstraintName("FK_SC_Course");

                entity.HasOne(d => d.SnoNavigation)
                    .WithMany(p => p.Scs)
                    .HasForeignKey(d => d.Sno)
                    .HasConstraintName("FK_SC_Student");
            });

            modelBuilder.Entity<Student>(entity =>
            {
                entity.HasKey(e => e.Sno);

                entity.ToTable("Student");

                entity.Property(e => e.Sno)
                    .HasMaxLength(9)
                    .IsFixedLength(true);

                entity.Property(e => e.Sdeptno)
                    .HasMaxLength(2)
                    .IsFixedLength(true);

                entity.Property(e => e.Sname).HasMaxLength(20);

                entity.Property(e => e.Ssex)
                    .HasMaxLength(1)
                    .IsFixedLength(true);
            });



            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
